import scrapy
from ..items import MenuItem
import logging
class GetmenuSpider(scrapy.Spider):
    name = "getmenu"
    allowed_domains = ["https://www.laserfocusworld.com.cn"]
    start_urls = ["https://www.laserfocusworld.com.cn/MBAP.asp"]  # 爬取的目标网址

    def parse(self, response):
        ##获取主题阅读下二级标题内的所有详情链接
        urls = response.xpath('//div[@id="centernew_05"]/a/@href').extract()  # 菜谱详情的链接
        for url in urls:
            yield scrapy.Request('https://www.laserfocusworld.com.cn/'+url, callback=self.parse2, dont_filter=True)  # 提交url到下一层

    def parse2(self, response):
        menuitem = MenuItem()
        # # 菜谱名
        menuitem['name'] = response.xpath('//div[@id="centernew_03"]/table[1]/tr[2]/td').extract()
        menuitem['energy'] = response.xpath('//div[@id="centernew_03"]/div[@id="centernew_06"]//table[1]/tr[1]/td').extract()
        menuitem['sugar'] = response.xpath(
            '//div[@id="centernew_03"]/div[2]/div[1]/p').extract()
        print(menuitem)
        # for info in menuitem:
        #     # 清理文本（可选），例如去除多余的空格或换行符
        #     # cleaned_text = info.strip()
        #     print(info.xpath('.//p/text()'))
